// server.js
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const config = require("./configs.js");

const app = express();
const Cookie = config.cookie;

const port = process.env.PORT;
const baseUrl = process.env.BASE_URL;

// 启用CORS
app.use(cors());
// 解析JSON请求体
app.use(express.json());

// 快码设置  --- 新增快码
app.post("/sysParam/saveSysParam", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/sysParam/saveSysParam`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );

    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 功能设置  ---  新增功能
app.post("/auth/addFunctionAction", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/auth/addFunctionAction`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );

    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 角色管理 --- 获取列表数据源
app.post("/sysRole/findSysFunctionPage", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/sysRole/findSysFunctionPage`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );

    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 角色管理 --- 新增JAVA功能勾选
app.post("/sysRole/roleFuntionCreateOrDelete", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/sysRole/roleFuntionCreateOrDelete`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 功能同步  ---  获取列表数据源
app.post("/sys/function/listPage", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/sys/function/listPage`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 快码同步  ---  获取列表数据源
app.post("/sysParam/findListPage", async (req, res) => {
  try {
    const response = await axios.post(
      `${baseUrl}/sysParam/findListPage`,
      req.body,
      {
        headers: {
          "Content-Type": req.headers["content-type"] || "application/json",
          Cookie,
        },
      }
    );
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 同步  ---  根据传参进行对应功能同步
app.post("/sync/syncData", async (req, res) => {
  try {
    const response = await axios.post(`${baseUrl}/sync/syncData`, req.body, {
      headers: {
        "Content-Type": req.headers["content-type"] || "application/json",
        Cookie,
      },
    });
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 获取菜单数据
app.get("/sysMenu/menuList", async (req, res) => {
  try {
    const response = await axios.get(`${baseUrl}/sysMenu/menuList`, {
      params: {}, // 传递空对象作为查询参数
      headers: {
        "Content-Type": req.headers["content-type"] || "application/json",
        Cookie,
      },
    });
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 获取菜单层级数据
app.get("/sysMenu/parentMenu", async (req, res) => {
  try {
    const response = await axios.get(`${baseUrl}/sysMenu/parentMenu`, {
      params: {}, // 传递空对象作为查询参数
      headers: {
        "Content-Type": req.headers["content-type"] || "application/json",
        Cookie,
      },
    });
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 获取菜单序号数据
app.post("/sysMenu/listPage", async (req, res) => {
  try {
    const response = await axios.post(`${baseUrl}/sysMenu/listPage`, req.body, {
      headers: {
        "Content-Type": req.headers["content-type"] || "application/json",
        Cookie,
      },
    });
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

// 新增菜单
app.post("/sysMenu/save", async (req, res) => {
  try {
    const response = await axios.post(`${baseUrl}/sysMenu/save`, req.body, {
      headers: {
        "Content-Type": req.headers["content-type"] || "application/json",
        Cookie,
      },
    });
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: "获取失败" });
  }
});

app.listen(port, () => {
  console.log(`BFF服务运行在 http://localhost:${port}`);
});
